package com.whc.gulimall.product.app;

import com.whc.common.utils.PageUtils;
import com.whc.common.utils.R;
import com.whc.gulimall.product.entity.CategoryEntity;
import com.whc.gulimall.product.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品三级分类
 *
 * @author wanghaochen
 * @email angaoen@outlook.com
 * @date 2022-03-05 00:18:53
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 获取所有分类
     *
     * @return 结果
     */
    @GetMapping("/listTree")
    public R listTree() {
        List<CategoryEntity> categoryEntityList = categoryService.listTree();
        return R.ok().put("data", categoryEntityList);
    }

    /**
     * 逻辑删除分类
     *
     * @param ids 分类的id
     * @return 结果
     */
    @DeleteMapping("/removeMenuByIds")
    public R removeMenuByIds(@RequestBody List<Long> ids) {
        categoryService.removeMenuByIds(ids);
        return R.ok();
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     *
     * @param category 分类
     * @return 统一返回结果
     */
    @PutMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateCascade(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
